package com.hz.friends.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.hisun.phone.core.voice.CCPCall;
import com.hz.friends.application.CustomApplication;
import com.hz.friends.chat.Helper.CCPHelper;
import com.hz.friends.chat.Helper.CCPUtil;
import com.hz.friends.serverrequest.ALEngineConfig.CallBack;
import com.hz.friends.serverrequest.ALCCPEngine;
import com.hz.friends.serverrequest.ALUserEngine;
import com.hz.friends.utils.MLog;

public class RegisterActivity extends BaseActivity implements CCPHelper.RegistCallBack{

	private EditText edtuname; // 名字
	private EditText edtupasswd; // 密码
	private EditText edtureppasswd; // 重复密码
	private EditText edtemail; // 邮箱
	private Button txvreturn;
	private final String TAG = "RegisterActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initViews();
	}

	@Override
	protected void init() {

	}

	@Override
	protected void initViews() {
		edtuname = (EditText) findViewById(R.id.edtuname);
		edtupasswd = (EditText) findViewById(R.id.edtupasswd);
		edtureppasswd = (EditText) findViewById(R.id.edtureppasswd);
		edtemail = (EditText) findViewById(R.id.edtemail);
		txvreturn = (Button) findViewById(R.id.txvreturn);
		txvreturn.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); // 下划线
		// txvreturn.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
	}

	@Override
	protected void initEvents() {

	}

	@Override
	protected void setViews() {

	}

	// 注册
	public void onregister(View view) {
		register();
		//资料完善页
//		Bundle bundle = new Bundle();
//		bundle.putInt("pageflag", 1);
//		startActivity(EditUserInfosActivity.class, bundle);
	}

	// 返回登录
	public void onreturn(View view) {
		startActivity(LoginActivity.class);
		finish();
	}

	private boolean validateInput() {

		String unmae = edtuname.getText().toString().trim();
		String upasswd = edtupasswd.getText().toString().trim();
		String ureppasswd = edtureppasswd.getText().toString().trim();
		String email = edtemail.getText().toString().trim();

		if (TextUtils.isEmpty(unmae)) {
			showToast("请输入用户名");
			return false;
		}

		if (TextUtils.isEmpty(upasswd)) {
			showToast("请输入密码");
			return false;
		}

		if (TextUtils.isEmpty(ureppasswd)) {
			showToast("请输入确认密码");
			return false;
		}
		if (!upasswd.equals(ureppasswd)) {
			showToast("两次密码输入不一致");
			return false;
		}
		if (TextUtils.isEmpty(email)) {
			showToast("请输入邮箱");
			return false;
		}

		if (!validateMail(email)) {
			showToast("请输入正确的邮箱");
			return false;
		}

		return true;
	}

	//注册
	public void register() {
		if (!validateInput()) {
			return;
		}
		
	 final 	String unmae = edtuname.getText().toString().trim();
	 final String upasswd = edtupasswd.getText().toString().trim();
	 final 	String email = edtemail.getText().toString().trim();
	createWiatDialog("正在注册");
	 ALUserEngine.defauleEngine().signUp(unmae, upasswd, email, new CallBack() {
		
		@Override
		public void done(Object object, AVException avexception) {
			cancleWaitDialog();
			if(avexception == null){//注册成功
				showToast("注册成功");
				CCPHelper.getInstance().setCountInit(3);
			      CCPHelper.getInstance().initVoipDevice();
				Bundle bundle = new Bundle();
				bundle.putInt("pageflag", 1);
				((CustomApplication) getApplication()).user = AVUser.getCurrentUser();
				startActivity(EditUserInfosActivity.class, bundle);
				finish();
			}else{//失败
				showToast("注册失败"+getregisterErrorMsg(avexception.getCode()));
				avexception.printStackTrace();
			}
		}
	});
	 
	 
		
//		putAsyncTask(new AsyncTask<Void, Void, Boolean>() {
//
//			@Override
//			protected void onPreExecute() {
//				super.onPreExecute();
//			}
//
//			@Override
//			protected Boolean doInBackground(Void... params) {
//				try {
//					Thread.sleep(1000);
//					return true;
//				} catch (InterruptedException e) {
//
//				}
//				return false;
//			}
//
//			@Override
//			protected void onPostExecute(Boolean result) {
//				super.onPostExecute(result);
//				if (result) {
//					startActivity(MaintabsActivity.class);
//					finish();
//				} else {
//
//				}
//			}
//		});

	}
	
	/**
	 * 验证邮箱地址是否正确
	 * 
	 * @param email
	 * @return
	 */
	public boolean validateMail(String email) {
		boolean flag = false;
		try {
			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) {
			e.printStackTrace();
			MLog.e(TAG, "验证邮箱地址错误");
		}
		return flag;
	}
	  //错误信息
		private String getregisterErrorMsg(int code) {
			String msg = "";
			if (code == 200) {
				msg = getString(R.string.loar_regerrortips_200);
			} else if (code == 201) {
				msg = getString(R.string.loar_regerrortips_201);
			} else if (code == 202) {
				msg = getString(R.string.loar_regerrortips_202);
			} else if (code == 203) {
				msg = getString(R.string.loar_regerrortips_203);
			} else if (code == 204) {
				msg = getString(R.string.loar_regerrortips_204);
			}
			return msg;
		}
		
	    //连接云通讯服务器回调
		@Override
		public void onRegistResult(final int reason, final String msg) {

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
//						 cancleWaitDialog();
						if (reason == CCPHelper.WHAT_ON_CONNECT) { //连接成功
//							showToast("注册成功");
							//资料完善页
					
						} else if (reason == CCPHelper.WHAT_ON_DISCONNECT || reason == CCPHelper.WHAT_INIT_ERROR) { //连接失败或初始失败
							// do nothing ...
//							showToast("注册失败  "+msg);
//							showToast("注册失败  "+msg);
							CCPUtil.exitCCPDemo();
						} else {
							CCPUtil.exitCCPDemo();
//							showToast("注册错误");
							
						}
					CCPHelper.getInstance().setRegistCallback(null);
				}
			});
		
			
		}
}
